package com.micro.service.configuration;

import com.micro.service.filter.CustomFilter;
import com.micro.service.routeLocator.CustomRouteLocator;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Create by Vic Xu on 2019/3/20
 */
@Configuration
public class CommonConfiguration {

    @Autowired
    ZuulProperties zuulProperties;
    @Autowired
    ServerProperties server;

    @Bean
    public CustomFilter customFilter() {
        return new CustomFilter();
    }

    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registration.setLoadOnStartup(1);
        registration.addUrlMappings("/actuator/hystrix.stream");
        registration.setName("HystrixMetricsStreamServlet");
        return registration;
    }

    @Bean
    public CustomRouteLocator getCustomRouteLocator() {
        return  new CustomRouteLocator(server.getServlet().getServletPrefix(), zuulProperties);
    }
}
